﻿/*
 * ============ [ 废土 ] ============
 *
 * [ 维护者 ]  argoran@gmail.com
 * [  状态  ]  原型
 *
 * ============ [ 注释 ] ============
 *
 * 处理13号消息的类
 *
 * ==================================
 */

using System;

using LastLand.Data.Global;

using Up2Link.SegString;
using Up2Link.Server;
using Up2Link.Util;

namespace LastLand.Core.MsgProcessor
{
    public class CMsgLogin : IMessageProcessor
    {
        public void ProcessMessage(CMessagePack<CMessageSS> _msgPack)
        {
            CMessageSS msg = _msgPack.message;
            Int32 index = _msgPack.index;

            String[] data = msg.MsgContent.Split(CConst.DELIMITER_USERACCOUNT);
            String username = data[0];
            String password = data[1];

            if (!CData.connections[index].IsLoggedIn()                                       //客户端连接尚未登录
                && CData.userAccounts.ContainsKey(username)                                  //客户端指定的用户名存在
                && !CData.userAccounts[username].IsBinded()                                  //客户端指定的用户尚未登录
                && CMD5Hash.VerifyMD5Hash(password, CData.userAccounts[username].Password))  //客户端指定的密码正确
            {
                CData.connections[index].Status = CConst.USER_STATUS_LOGGED_IN;
                CData.connections[index].Account = username;
                CData.userAccounts[username].ConnectionBinding = index;
                CMessageSS okLoginMsg = new CMessageSS(CConst.SERVER_MESSAGE_COMPLETE_LOGIN, 0, CMessageSS.ENCODING_UTF32, "");
                CMessagePack<CMessageSS> okLoginMsgPack = new CMessagePack<CMessageSS>(okLoginMsg, index);
                CData.server.Send(okLoginMsgPack);
            }
            else if (CData.userAccounts.ContainsKey(username) && CData.userAccounts[username].IsBinded())
            {
                CMessageSS loggedInByOtherMsg = new CMessageSS(CConst.SERVER_MESSAGE_ACCOUNT_LOGGED_IN_BY_ANOTHER_CLIENT, 0, CMessageSS.ENCODING_UTF32, "");
                CMessagePack<CMessageSS> loggedInByOtherMsgPack = new CMessagePack<CMessageSS>(loggedInByOtherMsg, index);
                CData.server.Send(loggedInByOtherMsgPack);
            }
            else if (!CData.userAccounts.ContainsKey(username)
                || !CMD5Hash.VerifyMD5Hash(password, CData.userAccounts[username].Password))
            {
                CMessageSS invalidAuthMsg = new CMessageSS(CConst.SERVER_MESSAGE_INVALID_USERNAME_OR_PASSWORD, 0, CMessageSS.ENCODING_UTF32, "");
                CMessagePack<CMessageSS> invalidAuthMsgPack = new CMessagePack<CMessageSS>(invalidAuthMsg, index);
                CData.server.Send(invalidAuthMsgPack);
            }
            else
            {
                CMessageSS loggedInMsg = new CMessageSS(CConst.SERVER_MESSAGE_INVALID_USERNAME_OR_PASSWORD, 0, CMessageSS.ENCODING_UTF32, "");
                CMessagePack<CMessageSS> loggedInMsgPack = new CMessagePack<CMessageSS>(loggedInMsg, index);
                CData.server.Send(loggedInMsgPack);
            }
        }
    }
}
